package mvpfoot.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import mvpfoot.entity.Constant;

/**
 * Created by Chen qin lang on 2017/1/11.
 */
public class BaseUtil {

   /* public static final void saveObject(String path, Object saveObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(saveObject);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
*/
   public static final void saveObject(String path, Object saveObject) {
       FileOutputStream fos = null;
       ObjectOutputStream oos = null;
       File f = new File(path);
       try {
           fos = new FileOutputStream(f);
           oos = new ObjectOutputStream(fos);
           oos.writeObject(saveObject);
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (oos != null) {
                   oos.close();
               }
               if (fos != null) {
                   fos.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }

    public static Object restoreObject(String fileName){
        Object o=null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        File file = new File(fileName);
        if (!file.exists()){
            return null;
        }
        try {

            fis=new FileInputStream(file);
            ois=new ObjectInputStream(fis);
            o=ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

                try {
                    if (fis!=null){
                    fis.close();
                    }
                    if (ois!=null){
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return o;
    }
}
